<template>
  <div className="app-container">
    <div className="table-container">
      <base-list
        ref="table"
        url="/book_info"
        :options="options"
        :columns="columns"
        :format="format"
        :rules="rules"
        show-select
        permission="ADMIN"
        @reloadOptions="handleGetOptions"
        @selectChange="tableSelectChange"
        @editData="editData"
      >
        <template slot="header_actions" slot-scope="scope">
          <el-popconfirm
            title="是否刷新ES？"
            icon="el-icon-finished"
            icon-color="orange"
            confirm-button-type="warning"
            @confirm="refreshEs()">
            <el-button slot="reference" v-waves type="warning" icon="el-icon-refresh" style="margin-right: 10px">刷新ES</el-button>
          </el-popconfirm>
          <el-button v-waves type="info" class="filter-button" icon="el-icon-download" @click="addSpider()">爬书
          </el-button>
          <el-popconfirm
            title="是否合并主ID？"
            icon="el-icon-finished"
            icon-color="red"
            confirm-button-type="danger"
            @confirm="mergeRootId()">
            <el-button slot="reference" v-waves type="danger" icon="el-icon-finished" style="margin-left: 10px">合并主ID</el-button>
          </el-popconfirm>
        </template>
        <template slot="raw_actions" slot-scope="scope">
          <el-button v-waves type="primary" class="filter-button" style="margin-left: 10px" icon="el-icon-refresh"
                     @click="kimiExpand(scope.row)">KIMI扩展
          </el-button>
          <el-button v-waves type="success" class="filter-button" icon="el-icon-list" @click="updateSection(scope.row, scope.$index)">更新目录
          </el-button>
          <el-button v-waves type="success" class="filter-button" icon="el-icon-list" @click="updateWeight(scope.row, scope.$index)">更新权重
          </el-button>
        </template>
      </base-list>

      <el-drawer
        :visible.sync="drawerKimiVisible"
        :destroy-on-close="true"
        :close-on-press-escape="false"
        :wrapper-closable="false"
        direction="rtl"
        :modal="false"
        :size="$store.state.app.device !== 'mobile' ? '60%' : '100%'"
      >
        <el-form ref="kimiTempDataForm" label-width="auto" label-suffix=": " :model="kimiTemp" :rules="kimiExpandRules"
                 style="margin: 15px;">
          <el-form-item label="问题" prop="q">
            <el-input v-model="kimiTemp.q" clearable/>
          </el-form-item>
          <el-form-item label="结果" prop="expand">
            <el-input v-model="kimiTemp.expand" type="textarea" :rows="30" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button
              v-waves
              type="primary"
              :loading="addOrEditLoading"
              @click="handleSaveKimiExpand()"
            >保存
            </el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
      <el-drawer
        :visible.sync="drawerSectionVisible"
        :destroy-on-close="true"
        :close-on-press-escape="false"
        :wrapper-closable="false"
        direction="rtl"
        :modal="false"
        :size="$store.state.app.device !== 'mobile' ? '60%' : '100%'"
      >
        <el-form ref="sectionTempDataForm" label-width="auto" label-suffix=": " :model="kimiTemp" :rules="sectionRules"
                 style="margin: 15px;">
          <el-form-item label="图书" prop="q">
            <el-input v-model="sectionTemp.name" disabled clearable/>
          </el-form-item>
          <el-form-item label="章节" prop="expand">
            <el-input v-model="sectionTemp.sections" type="textarea" :rows="40" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button
              v-waves
              type="primary"
              :loading="addOrEditLoading"
              @click="handleSaveSection()"
            >保存
            </el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
      <el-drawer
        :visible.sync="drawerSpiderVisible"
        :destroy-on-close="true"
        :close-on-press-escape="false"
        :wrapper-closable="false"
        direction="rtl"
        :modal="false"
        :size="$store.state.app.device !== 'mobile' ? '60%' : '100%'"
      >
        <el-form ref="spiderTempDataForm" label-width="auto" label-suffix=": " :model="spiderTemp" :rules="rules.add"
                 style="margin: 15px;">
          <el-form-item label="书名/ISBN" prop="name">
            <el-input v-model="spiderTemp.name" class="form-item" clearable/>
          </el-form-item>
          <el-form-item label="作者" prop="author">
            <el-input v-model="spiderTemp.author" class="form-item" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button
              v-waves
              type="primary"
              :loading="addOrEditLoading"
              @click="handleSaveSpider()"
            >保存
            </el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
      <el-drawer
        :visible.sync="drawerWeightVisible"
        :destroy-on-close="true"
        :close-on-press-escape="false"
        :modal="false"
        :wrapper-closable="false"
        direction="rtl"
        :size="$store.state.app.device !== 'mobile' ? '60%' : '100%'"
      >
        <el-form ref="weightTempDataForm" label-width="auto" label-suffix=": " :model="temp" :rules="rules.weight"
                 style="margin: 15px;">
          <el-form-item label="书名" prop="name">
            <el-input v-model="temp.name" class="form-item" clearable/>
          </el-form-item>
          <el-form-item label="外倾型（E）权重值" prop="eweight">
            <el-input-number v-model="temp.eweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="内倾型（I）权重值" prop="iweight">
            <el-input-number v-model="temp.iweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="实感型（S）权重值" prop="sweight">
            <el-input-number v-model="temp.sweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="直觉型（N）权重值" prop="nweight">
            <el-input-number v-model="temp.nweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="思维型（T）权重值" prop="tweight">
            <el-input-number v-model="temp.tweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="情感型（F）权重值" prop="fweight">
            <el-input-number v-model="temp.fweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="判断型（J）权重值" prop="jweight">
            <el-input-number v-model="temp.jweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="知觉型（P）权重值" prop="pweight">
            <el-input-number v-model="temp.pweight" class="form-item"/>
          </el-form-item>
          <el-form-item label="3岁以内权重值" prop="weight3">
            <el-input-number v-model="temp.weight3" class="form-item"/>
          </el-form-item>
          <el-form-item label="3-6岁以内权重值" prop="weight5">
            <el-input-number v-model="temp.weight5" class="form-item"/>
          </el-form-item>
          <el-form-item label="7-10岁以内权重值" prop="weight10">
            <el-input-number v-model="temp.weight10" class="form-item"/>
          </el-form-item>
          <el-form-item label="11-15岁以内权重值" prop="weight15">
            <el-input-number v-model="temp.weight15" class="form-item"/>
          </el-form-item>
          <el-form-item label="16-20岁以内权重值" prop="weight20">
            <el-input-number v-model="temp.weight20" class="form-item"/>
          </el-form-item>
          <el-form-item label="21-25岁以内权重值" prop="weight25">
            <el-input-number v-model="temp.weight25" class="form-item"/>
          </el-form-item>
          <el-form-item label="26-30岁以内权重值" prop="weight30">
            <el-input-number v-model="temp.weight30" class="form-item"/>
          </el-form-item>
          <el-form-item label="31-35岁以内权重值" prop="weight35">
            <el-input-number v-model="temp.weight35" class="form-item"/>
          </el-form-item>
          <el-form-item label="36-40岁以内权重值" prop="weight40">
            <el-input-number v-model="temp.weight40" class="form-item"/>
          </el-form-item>
          <el-form-item label="41-45岁以内权重值" prop="weight45">
            <el-input-number v-model="temp.weight45" class="form-item"/>
          </el-form-item>
          <el-form-item label="46-50岁以内权重值" prop="weight50">
            <el-input-number v-model="temp.weight50" class="form-item"/>
          </el-form-item>
          <el-form-item label="51-55岁以内权重值" prop="weight55">
            <el-input-number v-model="temp.weight55" class="form-item"/>
          </el-form-item>
          <el-form-item label="56-60岁以内权重值" prop="weight60">
            <el-input-number v-model="temp.weight60" class="form-item"/>
          </el-form-item>
          <el-form-item label="61-70岁以内权重值" prop="weight70">
            <el-input-number v-model="temp.weight70" class="form-item"/>
          </el-form-item>
          <el-form-item label="71-80岁以内权重值" prop="weight80">
            <el-input-number v-model="temp.weight80" class="form-item"/>
          </el-form-item>
          <el-form-item label="81-120岁以内权重值" prop="weight120">
            <el-input-number v-model="temp.weight120" class="form-item"/>
          </el-form-item>
          <el-form-item label="男性权重值" prop="weightMan">
            <el-input-number v-model="temp.weightMan" class="form-item"/>
          </el-form-item>
          <el-form-item label="女性权重值" prop="weightWoman">
            <el-input-number v-model="temp.weightWoman" class="form-item"/>
          </el-form-item>
          <el-form-item label="已婚权重值" prop="weightMarry">
            <el-input-number v-model="temp.weightMarry" class="form-item"/>
          </el-form-item>
          <el-form-item label="未婚权重值" prop="weightNoMarry">
            <el-input-number v-model="temp.weightNoMarry" class="form-item"/>
          </el-form-item>
          <el-form-item label="无孩子权重值" prop="weightNoChildren">
            <el-input-number v-model="temp.weightNoChildren" class="form-item"/>
          </el-form-item>
          <el-form-item label="有孩子权重值" prop="weightChildren">
            <el-input-number v-model="temp.weightChildren" class="form-item"/>
          </el-form-item>
          <el-form-item>
            <el-button
              v-waves
              type="primary"
              :loading="addOrEditLoading"
              @click="handleSaveWeight()"
            >保存
            </el-button>
          </el-form-item>
        </el-form>
      </el-drawer>

    </div>
  </div>
</template>

<script>
import {requestBase} from '@/api/common'
import BaseList from '@/components/BaseList/index.vue'

import permission from '@/directive/permission' // 权限判断指令
import waves from '@/directive/waves'
import RemoteSelect from "@/components/RemoteSelect/index.vue";
import UploadImage from "@/components/UploadImage/index.vue";
import ListRowEdit from "@/components/BaseList/components/ListRowEdit.vue"; // waves directive

export default {
  name: 'BookInfo',
  components: {ListRowEdit, UploadImage, RemoteSelect, BaseList},
  directives: {permission, waves},
  data() {
    return {

      columns: [
        {show: true, edit: false, width: 'auto', minWidth: '100', key: 'id', fixed: true, label: 'ID', tooltip1: 'ID', type: 'NUMBER', queryable: true, sortable: true },
        {show: true, edit: false, width: 'auto', minWidth: '100', key: 'rootId', fixed: true, label: '根ID', tooltip1: '根ID', type: 'NUMBER', queryable: true, sortable: true },
        {show: true, edit: true, width: 'auto', minWidth: '150', fixed: true, align: 'left', sortable: true, key: 'name', label: '书名', tooltip1: '书名', type: 'WORD', queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '80', fixed: true, align: 'center', key: 'status', label: '审核状态', tooltip1: '审核状态', type: 'DICTIONARY', optionKey: 'statusOptions', queryable: true, sortable: true },
        {show: true, edit: true, width: 'auto', minWidth: '50', key: 'rating', align: 'right', label: '评分', tooltip1: '评分', type: 'NUMBER', queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '100', key: 'rateUserNum', align: 'right', label: '评分人数', tooltip1: '评分人数', type: 'NUMBER', queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '100', key: 'sourceId', align: 'right', label: '源ID', tooltip1: '源ID', type: 'WORD', queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '200', key: 'authors', label: '作者', tooltip1: '作者', searchType: 'WORD', type: 'REMOTE_SELECT', remote: {url: '/author_info', label: 'name', multiple: true}, queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '200', key: 'translators', label: '译者', tooltip1: '译者', searchType: 'WORD', type: 'REMOTE_SELECT', remote: {url: '/author_info', label: 'name', multiple: true}, queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '100', key: 'image', label: '主图', tooltip1: '主图', type: 'IMAGE', imageSuffix: 'book-' },
        {show: true, edit: true, width: 'auto', minWidth: '300', key: 'types', label: '图书分类', tooltip1: '图书分类', searchType: 'WORD', type: 'REMOTE_SELECT', remote: {url: '/book_type', label: 'name', multiple: true}, queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '300', sortable: true, key: 'publication', label: '出版社', tooltip1: '出版社', type: 'WORD', queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '300', sortable: true, key: 'isbn', label: 'ISBN', tooltip1: 'ISBN', type: 'WORD', queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '300', sortable: true, key: 'publicYear', label: '出版时间', tooltip1: '出版时间', type: 'WORD', queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '200', key: 'series', label: '丛书', tooltip1: '丛书', searchType: 'WORD', type: 'REMOTE_SELECT', remote: {url: '/series_info', label: 'name', multiple: true}, queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '200', key: 'producers', label: '出品方', tooltip1: '出品方', searchType: 'WORD', type: 'REMOTE_SELECT', remote: {url: '/producer_info', label: 'name', multiple: true}, queryable: true },
        {show: true, edit: true, width: 'auto', minWidth: '300', sortable: true, key: 'description', label: '简介', tooltip1: '简介', type: 'LONG_WORD' },
        {show: true, edit: false, width: '100', key: 'sectionNum', align: 'right', label: '章节数量', tooltip1: '章节数量', type: 'NUMBER', queryable: true },
        // {show: false, edit: true, width: 'auto', minWidth: '200', key: 'sections', label: '章节', tooltip1: '章节', type: 'LONG_WORD' },
        {show: true, edit: true, width: '100', key: 'eweight', align: 'right', label: '外倾型（E）权重值', tooltip1: '外倾型（E）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'iweight', align: 'right', label: '内倾型（I）权重值', tooltip1: '内倾型（I）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'sweight', align: 'right', label: '实感型（S）权重值', tooltip1: '实感型（S）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'nweight', align: 'right', label: '直觉型（N）权重值', tooltip1: '直觉型（N）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'tweight', align: 'right', label: '思维型（T）权重值', tooltip1: '思维型（T）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'fweight', align: 'right', label: '情感型（F）权重值', tooltip1: '情感型（F）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'jweight', align: 'right', label: '判断型（J）权重值', tooltip1: '判断型（J）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'pweight', align: 'right', label: '知觉型（P）权重值', tooltip1: '知觉型（P）权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight3', align: 'right', label: '3岁以内权重值', tooltip1: '3岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight5', align: 'right', label: '3-6岁以内权重值', tooltip1: '3-6岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight10', align: 'right', label: '7-10岁以内权重值', tooltip1: '5-10岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight15', align: 'right', label: '11-15岁以内权重值', tooltip1: '10-15岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight20', align: 'right', label: '16-20岁以内权重值', tooltip1: '15-20岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight25', align: 'right', label: '21-25岁以内权重值', tooltip1: '20-25岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight30', align: 'right', label: '26-30岁以内权重值', tooltip1: '25-30岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight35', align: 'right', label: '31-35岁以内权重值', tooltip1: '30-35岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight40', align: 'right', label: '36-40岁以内权重值', tooltip1: '35-40岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight45', align: 'right', label: '41-45岁以内权重值', tooltip1: '40-45岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight50', align: 'right', label: '46-50岁以内权重值', tooltip1: '45-50岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight55', align: 'right', label: '51-55岁以内权重值', tooltip1: '50-55岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight60', align: 'right', label: '56-60岁以内权重值', tooltip1: '55-60岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight70', align: 'right', label: '61-70岁以内权重值', tooltip1: '60-70岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight80', align: 'right', label: '71-80岁以内权重值', tooltip1: '70-80岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weight120', align: 'right', label: '81-120岁以内权重值', tooltip1: '80-120岁以内权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weightMan', align: 'right', label: '男性权重值', tooltip1: '男性权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weightWoman', align: 'right', label: '女性权重值', tooltip1: '女性权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weightMarry', align: 'right', label: '已婚权重值', tooltip1: '已婚权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weightNoMarry', align: 'right', label: '未婚权重值', tooltip1: '未婚权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weightNoChildren', align: 'right', label: '无孩子权重值', tooltip1: '无孩子权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: true, edit: true, width: '100', key: 'weightChildren', align: 'right', label: '有孩子权重值', tooltip1: '有孩子权重值', type: 'NUMBER', queryable: false, sortable: true },
        {show: false, edit: false, width: 'auto', minWidth: '200', key: 'createTime', label: '创建时间', tooltip1: '创建时间', type: 'DATE_TIME' },
        {show: true, edit: false, width: 'auto', minWidth: '200', key: 'updateTime', label: '更新时间', tooltip1: '更新时间', type: 'DATE_TIME', queryable: true },
        {show: false, edit: false, width: 'auto', minWidth: '200', key: 'createUser', label: '创建人', tooltip1: '创建人', type: 'WORD' },
        {show: false, edit: false, width: 'auto', minWidth: '200', key: 'updateUser', label: '修改人', tooltip1: '修改人', type: 'WORD' },
      ],
      format: {
        authors: (data, index) => {
          return (data.authors || []).map(e => e.name + '(' + e.id + ')').join(',')
        },
        translators: (data, index) => {
          return (data.translators || []).map(e => e.name + '(' + e.id + ')').join(',')
        },
        series: (data, index) => {
          return (data.series || []).map(e => e.name + '(' + e.id + ')').join(',')
        },
        producers: (data, index) => {
          return (data.producers || []).map(e => e.name + '(' + e.id + ')').join(',')
        },
        types: (data, index) => {
          return (data.types || []).map(e => e.name + '(' + e.id + ')').join(',')
        },
        createUser: (data, index) => {
          return data.createUser ? data.createUser.nickName + '(' + data.createUser.id + ')' : ''
        },
        updateUser: (data, index) => {
          return data.updateUser ? data.updateUser.nickName + '(' + data.updateUser.id + ')' : ''
        }
      },
      rules: {
        add: {
          name: [{required: true, message: '书名必填!', trigger: 'blur'}],
        },
        edit: {
          id: [{required: true, message: '请选择数据!', trigger: 'change'}],
          name: [{required: true, message: '书名必填!', trigger: 'blur'}],
        },
        weight: {
          eweight: [{required: true, message: 'E权重值必填!', trigger: 'blur'}, {required: true, message: 'E权重值必填!', trigger: 'change'}],
          iweight: [{required: true, message: 'I权重值必填!', trigger: 'blur'}, {required: true, message: 'I权重值必填!', trigger: 'change'}],
          sweight: [{required: true, message: 'S权重值必填!', trigger: 'blur'}, {required: true, message: 'S权重值必填!', trigger: 'change'}],
          nweight: [{required: true, message: 'N权重值必填!', trigger: 'blur'}, {required: true, message: 'N权重值必填!', trigger: 'change'}],
          tweight: [{required: true, message: 'T权重值必填!', trigger: 'blur'}, {required: true, message: 'T权重值必填!', trigger: 'change'}],
          fweight: [{required: true, message: 'F权重值必填!', trigger: 'blur'}, {required: true, message: 'F权重值必填!', trigger: 'change'}],
          jweight: [{required: true, message: 'J权重值必填!', trigger: 'blur'}, {required: true, message: 'J权重值必填!', trigger: 'change'}],
          pweight: [{required: true, message: 'P权重值必填!', trigger: 'blur'}, {required: true, message: 'P权重值必填!', trigger: 'change'}],
          weight3: [{required: true, message: '3岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '3岁以内权重值必填!', trigger: 'change'}],
          weight5: [{required: true, message: '3-6岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '3-6岁以内权重值必填!', trigger: 'change'}],
          weight10: [{required: true, message: '7-10岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '7-10岁以内权重值必填!', trigger: 'change'}],
          weight15: [{required: true, message: '11-15岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '11-15岁以内权重值必填!', trigger: 'change'}],
          weight20: [{required: true, message: '16-20岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '16-20岁以内权重值必填!', trigger: 'change'}],
          weight25: [{required: true, message: '21-25岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '21-25岁以内权重值必填!', trigger: 'change'}],
          weight30: [{required: true, message: '26-30岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '26-30岁以内权重值必填!', trigger: 'change'}],
          weight35: [{required: true, message: '31-35岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '31-35岁以内权重值必填!', trigger: 'change'}],
          weight40: [{required: true, message: '36-40岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '36-40岁以内权重值必填!', trigger: 'change'}],
          weight45: [{required: true, message: '41-45岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '41-45岁以内权重值必填!', trigger: 'change'}],
          weight50: [{required: true, message: '46-50岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '46-50岁以内权重值必填!', trigger: 'change'}],
          weight55: [{required: true, message: '51-55岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '51-55岁以内权重值必填!', trigger: 'change'}],
          weight60: [{required: true, message: '56-60岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '56-60岁以内权重值必填!', trigger: 'change'}],
          weight70: [{required: true, message: '61-70岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '61-70岁以内权重值必填!', trigger: 'change'}],
          weight80: [{required: true, message: '71-80岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '71-80岁以内权重值必填!', trigger: 'change'}],
          weight120: [{required: true, message: '81-120岁以内权重值必填!', trigger: 'blur'}, {required: true, message: '81-120岁以内权重值必填!', trigger: 'change'}],
          weightMan: [{required: true, message: '男性权重值必填!', trigger: 'blur'}, {required: true, message: '男性权重值必填!', trigger: 'change'}],
          weightWoman: [{required: true, message: '女性权重值必填!', trigger: 'blur'}, {required: true, message: '女性权重值必填!', trigger: 'change'}],
          weightMarry: [{required: true, message: '已婚权重值必填!', trigger: 'blur'}, {required: true, message: '已婚权重值必填!', trigger: 'change'}],
          weightNoMarry: [{required: true, message: '未婚权重值必填!', trigger: 'blur'}, {required: true, message: '未婚权重值必填!', trigger: 'change'}],
          weightNoChildren: [{required: true, message: '无孩子权重值必填!', trigger: 'blur'}, {required: true, message: '无孩子权重值必填!', trigger: 'change'}],
          weightChildren: [{required: true, message: '有孩子权重值必填!', trigger: 'blur'}, {required: true, message: '有孩子权重值必填!', trigger: 'change'}],
        }
      },
      kimiExpandRules: {
        id: [{required: true, message: '请选择书籍!', trigger: 'change'}],
        expand: [{required: true, message: '请输入回答!', trigger: 'blur'}],
      },
      sectionRules: {
        sections: [{required: true, message: '请输入目录!', trigger: 'blur'}],
      },
      bookTypeRelations: [{}],
      multipleSelection: [],
      temp: {},
      tempIndex: -1,
      tempSection: '',
      addOrEditLoading: false,
      drawerKimiVisible: false,
      kimiTemp: {
        book: {},
        q: '',
        expand: ''
      },
      drawerSpiderVisible: false,
      sectionTemp: {
        id: '',
        sections: ''
      },
      drawerSectionVisible: false,
      spiderTemp: {
        name: '',
        author: ''
      },
      drawerWeightVisible: false,
      options: {
        statusOptions: [{id: 'AUDITING', name: '待审核', type: 'warning'}, {
          id: 'SUCCESS',
          name: '通过',
          type: 'primary'
        }, {id: 'REJECT', name: '未通过', type: 'danger'}],
        bookTypeOptions: [],
      },
      mergeLoading: false
    }
  },

  created() {
    this.handleGetOptions()
  },
  mounted() {
  },
  methods: {
    columnValChange(val, index, column) {
      this.$set(this.temp, column, val)
    },
    tableSelectChange(val) {
      this.multipleSelection = val
    },
    addSpider() {
      this.spiderTemp.name = ''
      this.spiderTemp.author = ''
      this.drawerSpiderVisible = true
    },
    mergeRootId() {
      if (!this.multipleSelection || !this.multipleSelection.length) {
        this.$notify.error('请选择数据!')
        return
      }
      let max = 0
      let high = this.multipleSelection[0]
      const ids = []
      this.multipleSelection.forEach(e => {
        if (e.rateUserNum > max && e.status === 'SUCCESS') {
          max = e.rateUserNum
          high = e
        }
        ids.push(e.id)
      })
      this.mergeLoading = true
      requestBase({url: '/book_info/merge_root_id', method: 'POST', data: {
        ids: ids,
        rootId: high.id
      }}).then(res => {
        this.mergeLoading = false
        this.$notify.success('合并成功!')
        this.$refs.table.fetchData()
      }).catch(() => {
        this.mergeLoading = false
      })
    },
    updateSection(item, index) {
      this.temp = item
      this.tempIndex = index
      this.sectionTemp.id = item.id
      this.sectionTemp.name = item.name

      const conditions = [{c: 'bookId', d: 'EQ', v: [item.id]}]
      requestBase({
        url: '/book_section',
        methods: 'GET',
        params: {
          page: 1,
          size: 1000,
          asc: 'number',
          conditions: JSON.stringify(conditions)
        }
      }).then(res => {
        const {data} = res.data
        if (data) {
          this.sectionTemp.sections = data.map(e => e.name).join('\n')
        }
      }).finally(() => {
        this.drawerSectionVisible = true
      })
    },
    updateWeight(item, index) {
      this.temp = item
      this.tempIndex = index
      this.drawerWeightVisible = true
    },
    handleSaveWeight() {
      this.addOrEditLoading = true
      this.$refs.weightTempDataForm.validate((valid) => {
        if (valid) {
          requestBase({url: '/book_info/set_weight', data: this.temp, method: 'POST'}).then(res => {
            this.drawerWeightVisible = false
            this.$notify.success('保存成功!')
            this.$refs.table.fetchData()
            this.addOrEditLoading = false
          }).catch(() => {
            this.addOrEditLoading = false
          })
        } else {
          this.addOrEditLoading = false
        }
      })
    },
    kimiExpand(item) {
      this.kimiTemp.book = item
      this.kimiTemp.q = '根据' + (item.authors ? item.authors[0].name : '') + '《' + item.name + '》以豆瓣、知乎、微信读书以及其他图书网站的相关信息，推荐10本书，并给出推荐原因。按照 \\n书名:《XXX》 作者:XXX\\n 推荐原因:XXX\\n 格式返回中文答案。'
      this.kimiTemp.expand = ''


      const input = document.createElement('input')
      input.value = this.kimiTemp.q
      document.body.appendChild(input)
      input.select()
      document.execCommand('copy')
      document.body.removeChild(input)
      this.$notify.success({message: '问题已复制到剪贴板', position: 'top-left'})
      this.drawerKimiVisible = true
    },
    refreshEs() {
      requestBase({
        url: '/book_info/reload_es',
        method: 'POST'
      }).then(res => {
        this.$notify.success('刷新成功!')
      })

    },
    handleSaveKimiExpand() {
      this.addOrEditLoading = true
      this.$refs.kimiTempDataForm.validate((valid) => {
        if (valid) {
          requestBase({url: '/book_info/kimi_expand', data: this.kimiTemp, method: 'POST'}).then(res => {
            this.drawerKimiVisible = false
            this.$notify.success('添加成功!')
            this.addOrEditLoading = false
          }).catch(() => {
            this.addOrEditLoading = false
          })
        } else {
          this.addOrEditLoading = false
        }
      })
    },
    handleSaveSection() {
      this.addOrEditLoading = true
      this.$refs.sectionTempDataForm.validate((valid) => {
        if (valid) {
          requestBase({url: '/book_info/sections', data: this.sectionTemp, method: 'POST'}).then(res => {
            this.drawerSectionVisible = false
            this.temp.sectionNum = res.data
            this.temp.status = 'SUCCESS'
            // this.$refs.table.saveSuccess(this.tempIndex, this.temp)
            this.$notify.success('保存成功!')
            this.addOrEditLoading = false
          }).catch(() => {
            this.addOrEditLoading = false
          })
        } else {
          this.addOrEditLoading = false
        }
      })
    },
    handleSaveSpider() {
      this.addOrEditLoading = true
      this.$refs.spiderTempDataForm.validate((valid) => {
        if (valid) {
          requestBase({url: '/book_info/spider', data: this.spiderTemp, method: 'POST'}).then(res => {
            this.drawerSpiderVisible = false
            this.$notify.success('添加成功!')
            this.addOrEditLoading = false
          }).catch(() => {
            this.addOrEditLoading = false
          })
        } else {
          this.addOrEditLoading = false
        }
      })
    },
    handleGetOptions() {
      const options = JSON.parse(localStorage.getItem('allOptions'))
      if (options) {
        this.options = Object.assign(this.options, options)
      }
      requestBase({url: '/book_type/options?show=id,name'}).then(res => {
        this.options.bookTypeOptions = res.data
      })
    }
  }
}
</script>


